﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class ViewBangKeKhaiPhiSuDungTaiLieu : Form
    {
        public ViewBangKeKhaiPhiSuDungTaiLieu()
        {
            InitializeComponent();
        }

        private void SetPreferences()
        {
            radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.ShowGroupPanel = false;
            radGridView1.MasterTemplate.EnableGrouping = false;
            radGridView1.EnableHotTracking = true;
            radGridView1.CommandCellClick += ViewDetail;
        }

        void ViewDetail(object sender, EventArgs e)
        {
            int id = Convert.ToInt32((sender as GridCommandCellElement).Value);
            Utilities.CloseForm(this, new DisplayBangKeKhaiPhiSuDung(id));
        }

        private void BuildData()
        {
            radGridView1.MasterTemplate.AllowAddNewRow = false;
            radGridView1.MasterTemplate.AutoGenerateColumns = false;

            var biz = new PhieuBanGiaoTaiLieuBiz();
            var tem = biz.ViewAll();
            radGridView1.MasterTemplate.DataSource = tem;

            if (tem.Count == 0)
            {
                buttonEdit.Enabled = false;
                buttonRemove.Enabled = false;
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = parent.Status.Text = Constants.FormStatus.PhieuBanGiao;
            }

            SetPreferences();
            BuildData();

            CheckSecurity();
        }

        private void Edit(object sender, EventArgs e)
        {
            var dataRowInfo = radGridView1.CurrentRow as GridViewDataRowInfo;
            if (dataRowInfo != null)
            {
                var id = (int) dataRowInfo.Cells[10].Value;

                Utilities.CloseForm(this, new NewBangKeKhaiPhiSuDung(id));
            }
        }

        private void Remove(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                var items = new List<PhieuBanGiaoTaiLieu>();

                foreach (var row in radGridView1.Rows)
                {
                    if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
                    {
                        items.Add(row.DataBoundItem as PhieuBanGiaoTaiLieu);
                    }
                }

                var biz = new PhieuBanGiaoTaiLieuBiz();
                biz.RemoveItem(items);
            }

            Utilities.CloseForm(this, new ViewBangKeKhaiPhiSuDungTaiLieu());
        }

        private void NewItem(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new NewBangKeKhaiPhiSuDung());
        }

        private void CheckSecurity()
        {
            var parrent = MdiParent as MDI;

            if (parrent == null) return;

            if (parrent.CurrentGuest != null)
            {
                buttonEdit.Visible = false;
                buttonRemove.Visible = false;
                buttonNew.Visible = false;
            }
        }

        private void ExportData(object sender, EventArgs e)
        {
            var dataRowInfo = radGridView1.CurrentRow as GridViewDataRowInfo;
            if (dataRowInfo != null)
            {
                var id = (int)dataRowInfo.Cells[5].Value;

                var biz = new PhieuBanGiaoTaiLieuBiz();
                //biz.Export(id, "1", string.Empty);
            }

            MessageBox.Show(Constants.Messages.ExportCompleted);
        }        
    }
}
